package jobhunter.service;

import jobhunter.model.Contact;
import jobhunter.repository.CompanyRepository;
import jobhunter.repository.ContactRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ContactService
{
	@Autowired
	private CompanyRepository companyRepository;
	@Autowired
	private ContactRepository contactRepository;

	public void update(Long id, String firstName, String lastName,
			String phone, String email, String function, String description, Long companyId)
	{
		Contact contact = contactRepository.findById(id);
		contact.setPhone(phone);
		contact.setDescription(description);
		contact.setEmail(email);
		contact.setFirstName(firstName);
		contact.setLastName(lastName);
		contact.setFunction(function);
		contact.setCompany(companyRepository.findById(companyId));
		contactRepository.merge(contact);	
	}

	public void add(String firstName, String lastName, String phone,
			String email, String function, String description, Long companyId)
	{

		Contact contact = new Contact(firstName, lastName, function, description, phone, email);
		contact.setCompany(companyRepository.findById(companyId));
		contactRepository.persist(contact);
		
	}

}
